#include "../include/rpc_client.h"

#include <muduo/net/EventLoop.h>
#include <iostream>
#include <nlohmann/json.hpp>
#include <muduo/base/Logging.h>

// 记录发送的RPC请求日志
void LogRequest(const std::string& method, const nlohmann::json& params) {
	// muduo 库自带日志库记录请求信息 方法名和参数
	LOG_INFO << "Sending RPC request for method: " << method << " with params: " << params.dump(); 
}

// 接收到来自服务端的响应日志
void LogResponse(const std::string& method, const nlohmann::json& response) {
	// 同上，只是记录的信息不同 都是日志库
	LOG_INFO << "Received response for method: " << method << " with ans: " << response.dump();
}

void test() {
	// 客户端与服务端通信 也需要事件循环
	muduo::net::EventLoop loop;

	// 实例对象创建 两个参数 IP 端口号
	// muduo::net::InetAddress::InetAddress(muduo::StringArg ip, uint16_t port, bool ipv6 = false)
	scRpc::RpcClient client(&loop, muduo::net::InetAddress("127.0.0.1", 8888));

	try {
		// 启动 测试服务端的两个注册方法
		nlohmann::json add_params = {10, 20};
		// client.Call("add", add_params);
		std::cout << "Add ans: " << client.Call("add", add_params)["ans"].get<int>() << std::endl;

		nlohmann::json concat_params;
		concat_params["str1"] = "scRpc test: ";
		concat_params["str2"] = "Final end";
		std::cout << "Concat ans: " << client.Call("add", add_params)["ans"].get<std::string>() << std::endl;

	} catch (const std::exception& e) {
		std::cerr << "Rpc call failed" << e.what() << std::endl;
	}
	
	loop.loop();
}

int main() {
	test();
	return 0;
}